import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class test1 {
    public static void main(String[] args) throws SQLException {
       /* Scanner sc=new Scanner(System.in);
        System.out.println("输入id");
        int id=sc.nextInt();
        System.out.println("输入名字");
        String name=sc.next();*/

        //建立数据源
        DataSource dataSource=new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/java109?characterEncoding=utf8&useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("642364");


        //和数据库服务器建立连接  后序才有请求和响应交互
        Connection connection=dataSource.getConnection();


        //构造SQL
        //String sql="delete from student where id=1";
        //String sql="insert into student values(1,'孙悟空') ";
        String sql="update student set name='猪八戒' where id=1";
        //进行预处理
        PreparedStatement statement=connection.prepareStatement(sql);
       /* statement.setInt(1,id);
        statement.setString(2,name);*/

        //把SQL发送给服务器
        int n=statement.executeUpdate();//影响的行数
        System.out.println("n="+n);
        //System.out.println(statement);

        //最后释放资源，最后获取的资源先释放
        statement.close();
        connection.close();
    }
}
